package com.tjoydoorsystem.view.camera;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.media.FaceDetector;
import android.os.Build;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


/**
 * Created by whz on 2016/11/8.
 */

public class FaceSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    protected SurfaceHolder sh;
    private int mWidth;
    private int mHeight;

    public FaceSurfaceView(Context context) {
        super(context);
        init();
    }

    public FaceSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FaceSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public FaceSurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public void init() {
        sh = getHolder();
        sh.addCallback(this);
        sh.setFormat(PixelFormat.TRANSPARENT);
        setZOrderOnTop(true);
    }


    public void surfaceChanged(SurfaceHolder arg0, int arg1, int w, int h) {
        mWidth = w;
        mHeight = h;
    }

    public void surfaceCreated(SurfaceHolder arg0) {

    }

    public void surfaceDestroyed(SurfaceHolder arg0) {

    }

    public void clearDraw() {
        /*对画布进行清屏*/
        Canvas canvas = sh.lockCanvas();
        if (canvas != null) {
            Paint paint = new Paint();
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            canvas.drawPaint(paint);
        }
        sh.unlockCanvasAndPost(canvas);
    }

    public void drawLine(float x1, float y1, float x2, float y2, double scale) {

        Canvas canvas = sh.lockCanvas();
        canvas.drawColor(Color.TRANSPARENT);
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setColor(Color.GREEN);
        p.setStyle(Paint.Style.STROKE);
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int stroke = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, metrics);
        p.setStrokeWidth(stroke);


        RectF rectF = new RectF();
        rectF.set(new RectF(
                (x1) * (float) scale,
                (y1) * (float) scale,
                (x2) * (float) scale,
                (y2) * (float) scale));

        canvas.drawRect(rectF, p);
        sh.unlockCanvasAndPost(canvas);

    }

    public void drawRectF(FaceDetector.Face face) {
        //
        Canvas canvas = sh.lockCanvas();
        canvas.drawColor(Color.TRANSPARENT);

        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        int stroke = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, metrics);
        paint.setStrokeWidth(stroke);

        PointF mid = new PointF();
        face.getMidPoint(mid);

        RectF rectF = new RectF();
        if (mid.x != 0.0f && mid.y != 0.0f) {
            float eyesDis = face.eyesDistance();
            rectF.set(new RectF(
                    (mid.x - eyesDis),
                    (mid.y - eyesDis),
                    (mid.x + eyesDis),
                    (mid.y + eyesDis)));
            canvas.drawRect(rectF, paint);
            sh.unlockCanvasAndPost(canvas);
        }
    }
}
